package top.yongyan0624.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import top.yongyan0624.common.lang.CodeMsg;
import top.yongyan0624.common.lang.ResultVO;
import top.yongyan0624.entity.*;
import top.yongyan0624.entity.CardInfo;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yongyan
 * @since 2021-05-16
 */
@RestController
public class CardInfoController extends BaseController {

    @GetMapping("/home/cardInfo")
    public ResultVO cardInfo() {
        List<CardInfo> cardInfos = cardInfoService.list();

        return ResultVO.success(cardInfos);
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @PostMapping("/admin/addCard")
    public ResultVO addCard(@RequestBody CardInfo cardInfo) {
        cardInfo.setTime(LocalDateTime.now());
        boolean ok = cardInfoService.save(cardInfo);
        
        if (ok){
            return ResultVO.success("新增成功！");
        }

        return ResultVO.error(new CodeMsg("对不起！出错了，新增失败！"));
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @PostMapping("/admin/delCard/{id}")
    public ResultVO delCard(@PathVariable Long id){
        boolean ok = cardInfoService.removeById(id);

        if (ok){
            return ResultVO.success("删除成功！");
        }

        return ResultVO.error(new CodeMsg("对不起！出错了，删除失败！"));
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @RequestMapping("/admin/delAllCard")
    public ResultVO delAllCard(@RequestBody MyList<Long> myList) {
        List<Long> ids = myList.getList();
        for (Long id : ids) {
            if (delCard(id).getCode() == 400) {
                return ResultVO.error(new CodeMsg("对不起！不存在这条记录:" + id + "，删除失败！"));
            }
        }
        return ResultVO.success("删除成功！");
    }

    @PreAuthorize("hasRole('superAdmin')")
    @Transactional
    @PostMapping("/admin/updateCard")
    public ResultVO updateCard(@RequestBody CardInfo cardInfo){
        cardInfo.setTime(LocalDateTime.now());
        boolean ok = cardInfoService.updateById(cardInfo);

        if (ok) {
            return ResultVO.success("修改成功！");
        }

        return ResultVO.error(new CodeMsg("对不起！出错了，修改失败！"));
    }

    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @RequestMapping("/admin/queryCard")
    public ResultVO queryCard(@RequestParam Long pageNum, @RequestParam Long pageSize) {
        Page<CardInfo> page = new Page<>(pageNum, pageSize);
        Page<CardInfo> pageData = cardInfoService.page(page, new QueryWrapper<>());

        return ResultVO.success(pageData);
    }

    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @RequestMapping("/admin/queryCardByName")
    public ResultVO queryCardByName(@RequestParam String name, @RequestParam Long pageNum, @RequestParam Long pageSize) {
        Page<CardInfo> page = new Page<>(pageNum, pageSize);
        Page<CardInfo> pageData = cardInfoService.page(page, new QueryWrapper<CardInfo>()
                .like(name != null && !name.equals(""), "name", name)
                .orderByAsc("name"));

        return ResultVO.success(pageData);
    }

}
